package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.service.IProductCategoryService;
import com.mdd.admin.validate.ProductCategoryCreateValidate;
import com.mdd.admin.validate.ProductCategorySearchValidate;
import com.mdd.admin.validate.ProductCategoryUpdateValidate;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.vo.ProductCategoryDetailVo;
import com.mdd.admin.vo.ProductCategoryListedVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.ProductCategory;
import com.mdd.common.mapper.ProductCategoryMapper;
import com.mdd.common.util.TimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * 产品分类实现类
 * @author LikeAdmin
 */
@Service
public class ProductCategoryServiceImpl implements IProductCategoryService {
        
    @Resource
    ProductCategoryMapper productCategoryMapper;

    /**
     * 产品分类列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<ProductCategoryListedVo>
     */
    @Override
    public PageResult<ProductCategoryListedVo> list(PageValidate pageValidate, ProductCategorySearchValidate searchValidate) {
        Integer page  = pageValidate.getPage_no();
        Integer limit = pageValidate.getPage_size();

        QueryWrapper<ProductCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc(Arrays.asList("sort", "id"));

        productCategoryMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:name:str",
            "=:icon:str",
            "=:sort:int",
            "=:status:int",
        });

        IPage<ProductCategory> iPage = productCategoryMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<ProductCategoryListedVo> list = new LinkedList<>();
        for(ProductCategory item : iPage.getRecords()) {
            ProductCategoryListedVo vo = new ProductCategoryListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime().getTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime().getTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 产品分类详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return ProductCategory
     */
    @Override
    public ProductCategoryDetailVo detail(Integer id) {
        ProductCategory model = productCategoryMapper.selectOne(
                new QueryWrapper<ProductCategory>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        ProductCategoryDetailVo vo = new ProductCategoryDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 产品分类新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(ProductCategoryCreateValidate createValidate) {
        ProductCategory model = new ProductCategory();
        model.setName(createValidate.getName());
        model.setIcon(createValidate.getIcon());
        model.setSort(createValidate.getSort());
        model.setStatus(createValidate.getStatus());
        model.setCreateTime(new Date());
        model.setUpdateTime(new Date());
        productCategoryMapper.insert(model);
    }

    /**
     * 产品分类编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(ProductCategoryUpdateValidate updateValidate) {
        ProductCategory model = productCategoryMapper.selectOne(
                new QueryWrapper<ProductCategory>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setName(updateValidate.getName());
        model.setIcon(updateValidate.getIcon());
        model.setSort(updateValidate.getSort());
        model.setStatus(updateValidate.getStatus());
        model.setUpdateTime(new Date());
        productCategoryMapper.updateById(model);
    }

    /**
     * 产品分类删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        ProductCategory model = productCategoryMapper.selectOne(
                new QueryWrapper<ProductCategory>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        productCategoryMapper.delete(new QueryWrapper<ProductCategory>().eq("id", id));
    }

}
